﻿using IRealize;
using Microsoft.AspNetCore.Mvc;
using Realize;

namespace WebApplication9.Controllers.ClientSet
{
    //职位设置
    public class ZWSZController : Controller
    {
        private readonly ILogger<TextController> _logger;
        private readonly IConfiguration _configuration;
        public ZWSZController(ILogger<TextController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> GetTitles()//查询数据添加到前台
        {
            try
            {
                IConfig_Major config_Major = new Config_Major();
                // 假设select_major_name返回职称列表
                List<Entity.Config_Major> zhi = await config_Major.select_major_name(_configuration["SqlServer"]);
                // 将数据映射为前端需要的格式

                return Ok(new
                {
                    list = zhi,
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取职称数据失败: {ex.Message}");
            }
        }

        [HttpDelete]
        public async Task<IActionResult> Delete(string id)//删除
        {
            try
            {
                IConfig_Major config_Major = new Config_Major();
                int result = await config_Major.delete(id, _configuration["SqlServer"]);
                return Ok(new
                {
                    zhi=result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"删除失败: {ex.Message}");
            }
        }
        [HttpGet]
        public IActionResult Add()//添加页面
        {
            return View();
        }

        public IActionResult Fan()//返回
        {
            return RedirectToAction("Index");
        }

        [HttpPost]
        public async Task<IActionResult> AddData(CM majorData)
        {
            try
            {
                Console.WriteLine(majorData.major_kind_name+"");
                IConfig_Major config_Major = new Config_Major();
                int result = await config_Major.Add(majorData.major_kind_id,majorData.major_id,majorData.major_kind_name,majorData.major_name, _configuration["SqlServer"]);

                if (result > 0)
                {
                    return Ok(new { success = true, message = "添加成功" });
                }
                else
                {
                    return Ok(new { success = false, message = "添加失败" });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加职位失败");
                return StatusCode(500, new { success = false, message = "添加失败: " + ex.Message });
            }
        }

        public async Task<IActionResult> select1()//第一个下拉框绑定数据
        {
            try
            {
                IConfig_Major_Zw config_Major = new Config_Major_Zw();
                List<Entity.Config_Major_Zw> zhi = await config_Major.select(_configuration["SqlServer"]);
                return Ok(new
                {
                    list = zhi
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取职称数据失败: {ex.Message}");
            }
        }

        public async Task<IActionResult> select2()//第二个下拉列表绑定数据
        {
            try
            {
                IConfig_Major_Kind config_Major_Kind = new Config_Major_Kind();
                List<Entity.Config_Major_Kind> zhi = await config_Major_Kind.selectCMK(_configuration["SqlServer"]);

                return Ok(new
                {
                    list = zhi
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取职称数据失败: {ex.Message}");
            }
        }
    }

    public class CM
    {
        public string major_kind_id { get; set; }
        public string major_kind_name { get; set; }
        public string major_id { get; set; }
        public string major_name { get; set; }
    }

}
